<?php
class ColgUtils {
  public static function bal_enrl($orm, $id) {
    $opts = new stdClass();
    $opts->sel = 'SUM(t.balance__) AS __bal';
    $opts->cd = 't.enrollment_id = :v1';
    $opts->cdv = array(':v1' => $id);
    $rslt = $orm->get('pb_colg_invoice', NULL, NULL, NULL, NULL, $opts)->d;
    $obj = new stdClass();
    $obj->id = $id;
    $obj->balance__ = ($rslt[0]->__bal) ? $rslt[0]->__bal : 0;
    $orm->update('pb_colg_enrollment', $obj);
    return $obj->balance__;
  }
  
  public static function bal_student($orm, $id) {
    $opts = new stdClass();
    $opts->sel = 'SUM(t.balance__) AS __bal';
    $opts->cd = 't.student_id = :v1';
    $opts->cdv = array(':v1' => $id);
    $rslt = $orm->get('pb_colg_invoice', NULL, NULL, NULL, NULL, $opts)->d;
    $obj = new stdClass();
    $obj->id = $id;
    $obj->balance__ = ($rslt[0]->__bal) ? $rslt[0]->__bal : 0;
    $orm->update('pb_colg_student', $obj);
    return $obj->balance__;
  }
  
  public static function get_pturl($app_data, $delimiter='&') {
    global $__inst;
    return  '____pt.php?__sys=' . $app_data->sys->php_name . $delimiter . "__inst=$__inst" . $delimiter;
  }
  
  public static function get_atdnurl($app_data, $delimiter='&') {
    global $__inst;
    return  '____atdn.php?__sys=' . $app_data->sys->php_name . $delimiter . "__inst=$__inst" . $delimiter;
  }
  
  public static function get_atdnurl2($app_data, $delimiter='&') {
    global $__inst;
    return  '____atdn2.php?__sys=' . $app_data->sys->php_name . $delimiter . "__inst=$__inst" . $delimiter;
  }
  
  public static function atdn_by_enrolled_class($orm, $id) {
    $opts = new stdClass();
    $opts->cd = 't.enrolled_class_id = :v1 AND t.status != -1';
    $opts->cdv = array(':v1' => $id);
    return ColgUtils::__atdn($orm->get('pb_colg_atdn', NULL, NULL, NULL, NULL, $opts)->d);
  }
  
  public static function __atdn($atdn) {
    $abs = 0;
    $ls = 0;
    foreach ($atdn as $itm) {
      if ($itm->status == 3) {
        $abs++;
      } else if ($itm->status == 4) {
        $ls++;
      }
    }
    
    if (count($atdn) == 0) {
      return -1;
    }
    
    return ((count($atdn) - ((int)($ls / 2) + $abs)) / count($atdn)) * 100;
  }
}
?>